/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    bool isBalanced(TreeNode* root) {
        if(root==nullptr)return true;
        return height(root)>0;
    }
    int height(TreeNode* root){
        if(root==nullptr)return 0;
        int left=height(root->left);
        int right=height(root->right);
        if(right==-1||left==-1||abs(left-right)>1){
            return -1;
        }else{
            return max(left,right)+1;
        }
    }
};